package thesis.filesharing.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import thesis.filesharing.common.exception.system.AppSystemException;
import thesis.filesharing.dao.DocumentFileDAO;
import thesis.filesharing.dao.util.EMF;
import thesis.filesharing.model.Clazz;
import thesis.filesharing.model.DocumentFile;

import com.google.appengine.api.datastore.Key;

@Repository
public class DocumentFileDAOImpl implements DocumentFileDAO {

	@Override
	public List<DocumentFile> saveDocumentFiles(List<DocumentFile> documentFiles, List<Key> classKeys) {
		EntityManager em = null;
		EntityTransaction transaction = null;
		try {
			em = EMF.get().createEntityManager();
			transaction = em.getTransaction();
			transaction.begin();
			
			for (DocumentFile f : documentFiles) {
				transaction.commit();
				transaction.begin();
				f.getClassKeys().addAll(classKeys);
				em.persist(f);
			}
			em.flush();
			
			List<Key> documentFileKeys = new ArrayList<Key>();
			for (DocumentFile f : documentFiles) {
				documentFileKeys.add(f.getDocumentFileKey());
			}
			
			Clazz c = null;
			for (Key k : classKeys) {
				transaction.commit();
				transaction.begin();
				c = em.find(Clazz.class, k);
				c.getDocumentFileKeys().addAll(documentFileKeys);
				em.merge(c);
			}
			em.flush();
			
			transaction.commit();
			return documentFiles;
		} catch (Exception ex) {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DocumentFile> listDocumentFilesOfClass(Key classKey) {
		EntityManager em = null;
		try {
			em = EMF.get().createEntityManager();
			Clazz c = em.find(Clazz.class, classKey);
			Set<Key> documentFileKeys = c.getDocumentFileKeys();
			
			if (documentFileKeys != null && !documentFileKeys.isEmpty()) {
				String jpaql = "SELECT f FROM DocumentFile f WHERE f.documentFileKey IN (:documentFileKeys) ORDER BY f.documentFileKey DESC";
				Query query = em.createQuery(jpaql);
				query.setParameter("documentFileKeys", documentFileKeys);
				return (List<DocumentFile>) query.getResultList();
			}
			return null;
			
//			List<DocumentFile> documentFiles = new ArrayList<DocumentFile>();
//			for (Key k : documentFileKeys) {
//				documentFiles.add(em.find(DocumentFile.class, k));
//			}
//			return documentFiles;
			
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@Override
	public DocumentFile findDocumentFile(Key documentFileKey) {
		EntityManager em = null;
		try {
			em = EMF.get().createEntityManager();
			return em.find(DocumentFile.class, documentFileKey);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}
}
